﻿using System.Collections.Generic;
using Entity;
using Utils;
using System.Linq;
using System;
using DataBaseConnect.IRepositories;

namespace DataBaseConnect.App
{
    public class ButtonService : IButtonService
    {
        //按钮管理仓储接口
        private readonly IButtonRepository _buttonRepository;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="userRepository">仓储对象</param>
        public ButtonService(IButtonRepository buttonRepository)
        {
            _buttonRepository = buttonRepository;
        }

        /// <summary>
        /// 新增按钮样式信息
        /// </summary>
        /// <param name="Input">按钮样式实体</param>
        /// <returns></returns>
        public ReceiveStatus AddButtonStyleMsg(ButtonStyle Input)
        {
            return _buttonRepository.AddButtonStyleMsg(Input);
        }

        /// <summary>
        /// 编辑按钮样式信息
        /// </summary>
        /// <param name="Input">按钮样式实体</param>
        /// <returns></returns>
        public ReceiveStatus EditButtonStyleMsg(ButtonStyle Input)
        {
            return _buttonRepository.EditButtonStyleMsg(Input);
        }

        /// <summary>
        /// 根据按钮样式id获取对应的按钮样式
        /// </summary>
        /// <param name="ButtonStyleId">按钮样式id</param>
        /// <returns></returns>
        public ButtonStyle GetButtonStyleIdMsg(int ButtonStyleId)
        {
            return _buttonRepository.GetButtonStyleIdMsg(f => f.ButtonStyleId == ButtonStyleId);
        }

        /// <summary>
        /// 获取所有按钮样式&&查询
        /// </summary>
        /// <param name="ButtonStyleName">按钮样式名称</param>
        /// <returns></returns>
        public List<ButtonStyle> GetButtonStyleList(string ButtonStyleName)
        {
            return _buttonRepository.GetButtonStyleList(ButtonStyleName);
        }

        /// <summary>
        /// 新增按钮信息
        /// </summary>
        /// <param name="Input">按钮信息实体</param>
        /// <returns></returns>
        public ReceiveStatus AddButtonMsg(Button Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _buttonRepository.Insert(Input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败：" + ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 编辑按钮信息
        /// </summary>
        /// <param name="Input">按钮信息实体</param>
        /// <returns></returns>
        public ReceiveStatus EditButtonMsg(Button Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _buttonRepository.Update(Input);
                receiveStatus.msg = "编辑成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg ="编辑失败：" + ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据菜单id查找按钮权限表
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public List<ButtonRole> GetButtonRoleByMenuIdList(int MenuId ,int RoleId)
        {
            return _buttonRepository.GetButtonRoleByMenuIdList(f => f.MenuId == MenuId && f.RoleId == RoleId);
        }

        /// <summary>
        /// 根据菜单id获得按钮
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <returns></returns>
        public List<Button> GetAllButtonMenuIdList(int MenuId)
        {
            return _buttonRepository.GetAllList(f => f.MenuId == MenuId).OrderBy(u=>u.OrderBy).ToList();
        }

        /// <summary>
        /// 根据按钮id获得按钮信息
        /// </summary>
        /// <param name="ButtonId">按钮id</param>
        /// <returns></returns>
        public Button GetAllButtonByButtonIdMsg(int ButtonId)
        {
            return _buttonRepository.GetAllList(f => f.ButtonId == ButtonId).FirstOrDefault();
        }
    }
}
